package org.skyeye.modeling.customerized.part;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.AccessibleAnchorProvider;
import org.eclipse.gef.AccessibleEditPart;
import org.eclipse.swt.accessibility.AccessibleEvent;
import org.skyeye.modeling.customerized.figures.FigureFactory;
import org.skyeye.modeling.customerized.figures.GPIOSocFigure;
import org.skyeye.modeling.customerized.model.GPIOSoc;
import org.skyeye.modeling.customerized.ui.LogicMessages;

public class GPIOSocEditPart extends LogicEditPart {

	protected AccessibleEditPart createAccessible() {
		return new AccessibleGraphicalEditPart() {

			public void getName(AccessibleEvent e) {
				e.result = LogicMessages.LogicPlugin_Tool_CreationTool_GPIOSOC_Label;
			}

			// public void getValue(AccessibleControlEvent e) {
			// e.result = Integer.toString(getGPIOSOCModel().getValue());
			// }

		};
	}

	protected void createEditPolicies() {
		super.createEditPolicies();
	}

	/**
	 * Returns a newly created Figure to represent this.
	 *
	 * @return Figure of this.
	 */
	protected IFigure createFigure() {
		return FigureFactory.createNewGPIOSoc(this.getGPIOSOCModel());
	}

	public Object getAdapter(Class key) {
		if (key == AccessibleAnchorProvider.class)
			return new DefaultAccessibleAnchorProvider() {
				public List getSourceAnchorLocations() {
					List list = new ArrayList();
					Vector sourceAnchors = getNodeFigure().getSourceConnectionAnchors();
					for (int i = 0; i < sourceAnchors.size(); i++) {
						ConnectionAnchor anchor = (ConnectionAnchor) sourceAnchors.get(i);
						list.add(anchor.getReferencePoint().getTranslated(0, -3));
					}
					return list;
				}

				public List getTargetAnchorLocations() {
					List list = new ArrayList();
					Vector targetAnchors = getNodeFigure().getTargetConnectionAnchors();
					for (int i = 0; i < targetAnchors.size(); i++) {
						ConnectionAnchor anchor = (ConnectionAnchor) targetAnchors.get(i);
						list.add(anchor.getReferencePoint().getTranslated(0, 3));
					}
					return list;
				}
			};
		return super.getAdapter(key);
	}

	/**
	 * Returns the Figure of this as a LEDFigure.
	 *
	 * @return LEDFigure of this.
	 */
	public GPIOSocFigure getGPIOSocFigure() {
		return (GPIOSocFigure) getFigure();
	}

	/**
	 * Returns the model of this as a LED.
	 *
	 * @return Model of this as an LED.
	 */
	protected GPIOSoc getGPIOSOCModel() {
		return (GPIOSoc) getModel();
	}

	public void propertyChange(java.beans.PropertyChangeEvent change) {
		if (getGPIOSOCModel().getValues().containsKey(change.getPropertyName())) {
			refreshVisuals();
		}

		else
			super.propertyChange(change);
	}

	/**
	 * Apart from the usual visual update, it also updates the numeric contents
	 * of the LED.
	 */
	public void refreshVisuals() {

		HashMap<String, Boolean> values = getGPIOSOCModel().getValues();
		for (String name : values.keySet()) {
			getGPIOSocFigure().setValue(name, values.get(name));
		}
		super.refreshVisuals();
	}

	public void setSelected(int i) {
		super.setSelected(i);
		refreshVisuals();
	}

}